<!--
 * @Author: sjt
 * @Date: 2022-10-25 13:18:12
 * @LastEditors: sjt
 * @LastEditTime: 2024-10-09 09:45:31
 * @Description: file content
-->
<template>
  <div class="barsChart" ref="btBarsChartRef"></div>
</template>

<script setup>
import { defineExpose, ref } from 'vue'
import * as echarts from 'echarts'
let charts = null
const btBarsChartRef = ref(null)
const initChart = (datas) => {
  let myDate = datas.map((item) => item.name)
  let numB = new Array(datas.length).fill(
    Math.min(...datas.map((item) => item.value)) / 50
  )
  let option = {
    tooltip: {
      show: false,
      trigger: 'axis',
      backgroundColor: 'rgba(17,95,182,0.5)',
      textStyle: {
        color: '#fff'
      },
      axisPointer: {
        type: 'shadow',
        textStyle: {
          color: '#fff'
        }
      }
    },
    grid: {
      borderWidth: 0,
      top: '18%',
      left: 0,
      right: '0%',
      bottom: '20%',
      textStyle: {
        color: '#fff'
      }
    },
    calculable: true,
    xAxis: [
      {
        type: 'category',
        splitLine: {
          show: false
        },
        axisTick: {
          show: false
        },
        splitArea: {
          show: false
        },
        axisLabel: {
          color: '#fff',
          fontSize: '12px'
        },
        axisLine: {
          show: true,
          lineStyle: {
            color: '#3C95E0',
            width: 1
          }
        },
        data: myDate
      }
    ],
    yAxis: {
      type: 'value',
      nameTextStyle: {
        color: '#fff',
        fontSize: 14
      },
      axisLine: {
        lineStyle: {
          color: 'rgba(255, 255, 255, 0.4)',
          width: 1,
          type: 'solid'
        },
        show: false
      },
      splitLine: {
        show: true,
        lineStyle: {
          color: 'rgba(67, 168, 253, 0.1)',
          type: 'solid'
        }
      },
      axisLabel: {
        show: false,
        color: '#fff',
        fontSize: '12px'
      },
      axisTick: {
        show: false
      }
    },

    series: [
      {		//顶部的白色横杠
        type: 'pictorialBar',
        symbol: 'rect',
        symbolSize: [20, 1],
        symbolOffset: [0, -1],
        symbolPosition: 'end',
        data: datas,
        itemStyle: {
          color: '#fff',
        },
      },
      {
        name: '',
        type: 'bar',
        stack: '总量',
        barMaxWidth: 20,
        barGap: '10%',
        itemStyle: {
          show: true,
          color: new echarts.graphic.LinearGradient(
            0,
            0,
            0,
            1,
            [
              {
                offset: 0,
                color: 'rgba(25, 152, 232, 1)'
              },
              {
                offset: 1,
                color: 'rgba(25, 152, 232, 0.1)'
              }
            ],
            false
          ),
          borderRadius: 0
        },
        label: {
          show: true,
          position: 'top',
          color: '#fff',
          fontSize: 12,
          formatter: function (p) {
            return p.value > 0 ? p.value : ''
          }
        },
        data: datas
      },
    ]
  }

  if (!charts) {
    charts = echarts.init(btBarsChartRef.value)
    charts.setOption(option)
  } else {
    charts.dispose()
    charts = echarts.init(btBarsChartRef.value)
    charts.setOption(option)
  }
  window.addEventListener('resize', () => {
    if (charts) {
      charts.resize()
    }
  })
}

defineExpose({
  initChart
})
</script>

<style lang="scss" scoped>
.barsChart {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
</style>
